/**********************************************************************************************************************
 * \file CCU6_PWM_Generation.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 *
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
 * derivative works of the Software, unless such copies or derivative works are solely in the form of
 * machine-executable object code generated by a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *********************************************************************************************************************/

/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "IfxCcu6_PwmHl.h"
#include "CCU6_PWM_Generation.h"

/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define CCU6_BASE_FREQUENCY	    100000000                                     /* CCU6 base frequency, in Hertz        */
#define PWM_PERIOD              (CCU6_BASE_FREQUENCY / PWM_FREQUENCY)-1       /* PWM signal period, in ticks          */

#define NUMBER_OF_CHANNELS      3
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
IfxCcu6_TimerWithTrigger Pwm3_timer;
IfxCcu6_PwmHl Pwm3_driver;

/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* Function to initialize the CCU6 module to generate PWM signals */
void InitCCU60_3PWM(void)
{
    boolean interruptState = IfxCpu_disableInterrupts();            /* Disable global interrupts                    */

    /* Timer configuration: timer used as counter */
    IfxCcu6_TimerWithTrigger_Config timerConf;
    IfxCcu6_TimerWithTrigger_initConfig(&timerConf, &MODULE_CCU60); /* Initialize the timer configuration with
                                                                     * default values                               */
    /* User timer configuration */
    timerConf.base.frequency = PWM_FREQUENCY;                       /* Set the desired frequency for the PWM signal */
    timerConf.base.countDir = IfxStdIf_Timer_CountDir_upAndDown;    /* Configure the timer to count up and down, in
                                                                     * order to generate center-aligned PWM signals */
    /* Initialize the timer driver */
    IfxCcu6_TimerWithTrigger_init(&Pwm3_timer, &timerConf);

    /* PWM High/Low driver configuration */
    IfxCcu6_PwmHl_Config pwmHlConf;
    IfxCcu6_PwmHl_initConfig(&pwmHlConf);                           /* Initialize the PwmHl configuration with
                                                                     * default values                               */
    /* User PWM High/Low driver configuration */
    pwmHlConf.timer = &Pwm3_timer;                                     /* Use the already configured timer             */
    pwmHlConf.base.channelCount = NUMBER_OF_CHANNELS;               /* Configure the driver to use use all three
                                                                     * compare modules available in T12             */
    pwmHlConf.base.deadtime = 3e-7;
    /* Assign output pins: generation of (complementary) signals */
    /* Low-side and high-side switches */
    pwmHlConf.cc0 = &IfxCcu60_CC60_P11_12_OUT;
    pwmHlConf.cc1 = &IfxCcu60_CC61_P11_11_OUT;
    pwmHlConf.cc2 = &IfxCcu60_CC62_P11_10_OUT;
    pwmHlConf.cout0 = &IfxCcu60_COUT60_P11_9_OUT;
    pwmHlConf.cout1 = &IfxCcu60_COUT61_P11_6_OUT;
    pwmHlConf.cout2 = &IfxCcu60_COUT62_P11_3_OUT;

    /* Initialize the PwmHl driver */
    IfxCcu6_PwmHl_init(&Pwm3_driver, &pwmHlConf);

    /* Instruct the driver to generate center aligned PWM signals */
    IfxCcu6_PwmHl_setMode(&Pwm3_driver, Ifx_Pwm_Mode_centerAligned);

    /* Set the duty cycles for the three channels */
    Ifx_TimerValue cmpValues[NUMBER_OF_CHANNELS];
    cmpValues[0] = 0;                          /* Set the compare value for channel 1          */
    cmpValues[1] = 0;                          /* Set the compare value for channel 2          */
    cmpValues[2] = 0;                          /* Set the compare value for channel 3          */

    Pwm3_driver.update(&Pwm3_driver, cmpValues);                          /* Apply the compare values                     */

    /* Update the timer.
     * This instruction enables the shadow transfer of the compare values, copying the compare values to the
     * compare registers */
    IfxCcu6_TimerWithTrigger_applyUpdate(Pwm3_driver.timer);

    /* Restore interrupts to their initial state */
    IfxCpu_restoreInterrupts(interruptState);

    IfxCcu6_TimerWithTrigger_run(&Pwm3_timer);
}
